//
//  AppDelegate.swift
//  Demo-04-TabBarTest
//
//  Created by 苏统世 on 16/5/9.
//  Copyright © 2016年 SuTon. All rights reserved.
//

import UIKit

import AFNetworking

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        
        /// 设置监听网络状态
        AFNetworkReachabilityManager.sharedManager().startMonitoring()
        
        // BasicVC 配置
        basicVCConfig()
        
        window = UIWindow(frame: UIScreen.mainScreen().bounds)

        // 测试 
//        window?.rootViewController = STHomeVC()
        
        window?.rootViewController = defaultVC()
        
        window?.makeKeyAndVisible()
        
        return true
    }
    /// 显示默认的控制器
    private func defaultVC() -> UIViewController {
        if !STUserAccountViewModel.shareManager.isUserLogin {
            // 用户没有登录
            return STTabBarVC()
        }
        // 能到这个地方来用户已经登录了
        if isNewVersion() {
            return STNewFunctionVC()
        } else {
            return STWelcomeVC()
        }
    }
    
    /// 切换window的根控制器
    func switchRootVC(controller: UIViewController) {
        // 将传入的控制器设置给window的根控制器
        window?.rootViewController = controller
    }
    
    // MARK: - 判断是否是新版本
    func isNewVersion()->Bool{
        // 1.获取当前的版本号
        let currentVersion=NSBundle.mainBundle().infoDictionary!["CFBundleShortVersionString"] as! String
//        STPrint(items: "\(currentVersion)")
        // 2.获取上次的版本号
        let sandboxVersionKey = "sandboxVersionKey"
        let sandboxVersion = NSUserDefaults.standardUserDefaults().stringForKey(sandboxVersionKey)
        // 3.进行比较
        let isNewVersion = currentVersion != sandboxVersion
        // 4.是新版,将当前的版本号保存到沙盒
        if isNewVersion {
            NSUserDefaults.standardUserDefaults().setObject(currentVersion, forKey: sandboxVersionKey)
            NSUserDefaults.standardUserDefaults().synchronize()
        }
        return isNewVersion
    }
// MARK: - BasicVC 配置
    func basicVCConfig(){
        UINavigationBar.appearance().tintColor=UIColor.orangeColor()
//        UINavigationBar.appearance().titleTextAttributes
    }
    
    func applicationWillResignActive(application: UIApplication) {
        // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
        // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
    }

    func applicationDidEnterBackground(application: UIApplication) {
        // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
        // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
    }

    func applicationWillEnterForeground(application: UIApplication) {
        // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
    }

    func applicationDidBecomeActive(application: UIApplication) {
        // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    }

    func applicationWillTerminate(application: UIApplication) {
        // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    }


}

